package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        log.info("员工列表查询条件：{}", param);
        // 调用Service层查询分页数据
        IPage<Emp> page = Page.of(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());
//        queryWrapper.like(Emp::getName , param.getName())
//                .eq(Emp::getDeptId, param.getGender())
//                .between(Emp::getEntryDate, param.getBegin(), param.getEnd());//未使用动态sql
        page = this.page(page, queryWrapper);
        List<Emp> emps = page.getRecords();
        List<Integer> deptIds = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        if (!deptIds.isEmpty()) {
            List<Dept> depts = deptService.listByIds(deptIds);
            emps.forEach(emp -> {
                depts.forEach(dept -> {
                    if ( emp.getDeptId() != null && emp.getDeptId().equals(dept.getId()) ) {
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }

        return new PageResult<>(page.getTotal(), emps);
    }

    // 新增员工

    @Override
    public void saveEmp(Emp emp) {
        //新增员工
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        this.save(emp);

        //新增员工的工作经历
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
        });
        empExprService.saveBatch(exprList);
    }
    //  删除员工
    @Transactional
    @Override
    public void deleteByIds(List<Integer> ids) {
        this.removeByIds(ids);

        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));

    }
    // 修改员工
    @Transactional
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);

        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null &&  !exprList.isEmpty()) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
        }
        empExprService.saveBatch(exprList);
    }


}
